import 'dart:convert';
import 'dart:io';
import 'dart:async';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_yres/tool/constant.dart';
import 'package:flutter_yres/tool/BlankToolBarTool.dart';
import 'package:dio/dio.dart';
import 'package:flutter_yres/tool/network/http_method.dart';
import 'package:flutter_yres/tool/network/http_url.dart';
import 'package:flutter_yres/tool/Toast.dart';

class BindReferrerPage extends StatefulWidget {
  BindReferrerPage({Key key}) : super(key: key);

  @override
  _BindReferrerPageState createState() => _BindReferrerPageState();
}

class _BindReferrerPageState extends State<BindReferrerPage> {
  BlankToolBarModel barModel = BlankToolBarModel();
  String token = "";
  String phone = "";
  ScrollController scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: new AppBar(
        title: new Text(
          "绑定推荐人",
          style: TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
              color: Colors.black),
        ),
        elevation: 0.5,
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
      ),
      body: BlankToolBarTool.blankToolBarWidget(context,
          model: barModel,
          body: Container(
              // child: SingleChildScrollView(
              //     controller: scrollController,
              child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                Container(
                  color: white,
                  alignment: Alignment.centerLeft,
                   margin: EdgeInsets.all(10),
                  child:  TextField(
                      style: TextStyle(
                          fontSize: ScreenUtil.getInstance()
                              .setSp(BIG_FONT_SIZE * 2 - 15),
                          color: black),
                      keyboardType: TextInputType.number,
                      maxLines: 1,
                      controller: TextEditingController.fromValue(
                          TextEditingValue(
                              text: phone, //判断keyword是否为空
                              // 保持光标在最后

                              selection: TextSelection.fromPosition(
                                  TextPosition(
                                      affinity: TextAffinity.downstream,
                                      offset: phone.length)))),
                      minLines: 1,
                      cursorColor: mainColor,
                      onChanged: (value) {
                        phone = value;
                      },
                      keyboardAppearance: Brightness.light,
                      decoration: new InputDecoration(
                        contentPadding: EdgeInsets.all(5),
                        hintText: "输入邀请码",
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                            color: Colors.black45),
                      ),
                    ),
                ),
                Container(
                   margin: EdgeInsets.all(10),
                  child: Text('推荐人绑定后不可修改。',style:TextStyle(fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.red)),
                ),
                new Container(
                  width: double.infinity,
                  margin:
                      EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_40)),
                  height: ScreenUtil.getInstance().setWidth(DP_90),
                  decoration: new BoxDecoration(
                      color: mainColor,
                      borderRadius: BorderRadius.circular(DP_80)),
                  padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
                  child: new FlatButton(
                    child: new Padding(
                      padding: EdgeInsets.all(0.0),
                      child: new Text(
                        "绑定",
                        style: new TextStyle(
                            color: Colors.white,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE)),
                      ),
                    ),
                    onPressed: () {
                      bind();
                    },
                  ),
                ),
              ]))),
    );
  }

  @override
  void initState() {
    super.initState();
    barModel.outSideCallback = focusNodeChange;
    setState(() {});
  }

  void focusNodeChange() {
    barModel.focusNodeListener();
    setState(() {});
  }

  @override
  void dispose() {
    barModel.removeFocusListeners();
    // TODO: implement dispose
    super.dispose();
  }

  bind() async {
    Map map = new Map();
    map['token'] = token;
    map['r_phone'] = phone;
    FormData fromData = FormData.fromMap(new Map<String, dynamic>.from(map));
    Response<String> response =
        await HttpUtil().post(Api.BaseUrl+'/app.php?c=User&a=bandingR', data:fromData);
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    print(userMap);
    // Fluttertoast.showToast(msg: msg);
    Toast.toast(context, msg: msg);
    if (code == 1 && mounted) {
      Navigator.of(context).pop();
    }else{

    }
  }
}
